/**
 * Created by Administrator on 2017/1/12.
 */
$(function () {
    typhoonMap.init("map");

    $(".tool_zoom_in").click(function () {
        typhoonMap.zoomIn();
    });
    $(".tool_zoom_out").click(function () {
        typhoonMap.zoomOut();
    });
    $(".tool_zoom_center").click(function () {
        typhoonMap.map.setView(typhoonMap.defCenter, typhoonMap.defZoom);
    });
    $(".tool_measure").click(function () {
        typhoonMap.measure();
        $(this).attr("id", "mselected");
    });

    $(".tool_change_map").click(function () {
        var layerName = $(this).attr('layer-name');
        typhoonMap.changeTilelayer(layerName);
    });

    //工具栏切换
    $(".toolslist_toggle").toggle(function () {
        $(this).addClass("toggle_btn_transform").siblings().hide();
    }, function () {
        $(this).removeClass("toggle_btn_transform").siblings().show();
    });

    //图例显示隐藏
    $(".lf_title").on("click", function () {
        var index = $(".lf_title").index($(this));
        var targetEl = $(".lf_c");
        if (!$(this).hasClass("lf_title_style")) {
            $(this).addClass("lf_title_style");
            $(targetEl[index]).show();
        } else {
            $(this).removeClass("lf_title_style");
            $(targetEl[index]).hide();
        }
    });

    //历史台风查询
    $(".query_typhoonall p").click(function () {

        if (!$(this).hasClass("selected")) {
            $(this).addClass("selected");
            $(".typhoon_history").show();
        } else {
            $(this).removeClass("selected");
            $(".typhoon_history").hide();
        }

    });
});

function drawBoundPoly() {
    var latlngs = [[40.0939,116.405], [ 39.7725,116.311], [ 39.7039,116.71], [ 40.0251,116.805]];
    // var latlngs = [[37, -109.05],[41, -109.03],[41, -102.05],[37, -102.04]];
    var polygon = L.polygon(latlngs, {color: '#8784F5',fillOpacity:0,weight:1.5});
    polygon.addTo(typhoonMap.t_layers);
// zoom the map to the polygon
    typhoonMap.map.fitBounds(polygon.getBounds());
}

function drawPoly(id,latlngs) {
    var polygon = L.polygon(latlngs, {color: '#8784F5',fillOpacity:0,weight:1.5});
    
    if (typhoonMap.t_layers != undefined && typhoonMap.t_layers[id] && typhoonMap.t_layers[id].t_layer_group) {
        typhoonMap.map.removeLayer(typhoonMap.t_layers[id].t_layer_group);
        delete typhoonMap.t_layers[id];
    }

    if (typhoonMap.t_layers == undefined) {
        typhoonMap.t_layers = {};
    }
    // "t_path_layer":{},"t_center_marker_layer":{},"t_path_point_layer":{},"t_f_path_layer":{},"t_f_path_point_layer":{}
    typhoonMap.t_layers[id] = {};
    typhoonMap.t_layers[id].t_layer_group = L.layerGroup();
    typhoonMap.t_layers[id].t_layer_group.addTo(typhoonMap.map);

    
    polygon.addTo(typhoonMap.t_layers[id].t_layer_group);
}

function drawImg(id,imgUrl,imgBounds) {

    typhoonMap.t_layers[id].t_img_layer_group = L.layerGroup();
    typhoonMap.t_layers[id].t_img_layer_group.addTo(typhoonMap.map);
    L.imageOverlay(imgUrl, imgBounds).addTo(typhoonMap.t_layers[id].t_img_layer_group);
}

function clearImg(id) {
    typhoonMap.t_layers[id].t_img_layer_group.clearLayers();
}

L.drawLocal = {  //绘图提示文字
    draw: {
        handlers: {
            circle: {
                tooltip: {
                    start: '单击并拖动绘制圆'
                },
                radius: '半径(1ft=0.3048m)'
            },
            polygon: {
                tooltip: {
                    start: '单击开始绘制形状',
                    cont: '点击继续绘制形状',
                    end: '单击第一个点或双击最后点完成绘制'
                }
            },
            polyline: {
                error: '<strong>错误:</strong> 无效的图形!',
                tooltip: {
                    start: '单击开始绘制线',
                    cont: '点击继续绘制线',
                    end: '点击最后一个点完成绘制'
                }
            },
            rectangle: {
                tooltip: {
                    start: '单击并拖动绘制矩形'
                }
            },
            simpleshape: {
                tooltip: {
                    end: '释放鼠标完成绘图'
                }
            }
        }
    }
};

function drawRectangle() {
    typhoonMap.drawn.drawRectangle(typhoonMap.map, null, function (e) {
        var lefttop = e.layer._latlngs[1];
        var rightbottom = e.layer._latlngs[3];
        $('#rect_maxlat').val(lefttop.lat.toFixed(3));
        $('#rect_minlon').val(lefttop.lng.toFixed(3));
        $('#rect_minlat').val(rightbottom.lat.toFixed(3));
        $('#rect_maxlon').val(rightbottom.lng.toFixed(3));
    });
}

function drawCircle() {
    typhoonMap.drawn.drawCircle(typhoonMap.map, null, function (e) {
        var r = e.layer._mRadius / 1000.00;
        var latlng = e.layer._latlng;
        $('#circle_r').val(r.toFixed(3));
        $('#circle_lat').val(latlng.lat.toFixed(3));
        $('#circle_lng').val(latlng.lng.toFixed(3));
    });
}

function drawPolygon() {
    typhoonMap.drawn.drawPolygon(typhoonMap.map, null, function (e) {
        var latlngs = e.layer._latlngs.toString().replace(new RegExp('LatLng', 'g'), "");
        $('#area_polygon').text(latlngs);
        $('#area_polygon_val').val(latlngs);
    });
}